package org.action.jfreechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.bean.O2rankgame;
import org.bean.O2ranktap;
import org.hibernate.type.IntegerType;
import org.jfree.chart.*;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;


import org.service.GameService;

import com.opensymphony.xwork2.ActionSupport;

public class PlayerCapableHistoryAction extends ActionSupport {

	private O2rankgame o2rankgame;;
	private O2ranktap o2ranktap;
	private GameService gameservice;
	private int uid;
	private JFreeChart chart;
	private double capable ;
	private double up;
	private double down;
	private double temp;
	private List<O2rankgame>list;

	public JFreeChart createChart(DefaultCategoryDataset defaultcategorydataset) {

		chart = ChartFactory.createLineChart("近二十天实力值浮动", "日期", "实力值",
				this.getDataset(), PlotOrientation.VERTICAL, true, true, false);
		chart.setBackgroundPaint(Color.BLACK);
		//chart.setTitle(new TextTitle("近一月实力值浮动", new Font("微软雅黑", Font.BOLD, 22) ) );
		
		// 设置标题的颜色  
		   TextTitle text = new TextTitle(list.get(1).getNickname()+" 近二十天实力值浮动");  
	      text.setPaint(new Color(255,255,255)); 
	      text.setFont(new Font("微软雅黑", Font.BOLD, 22));
	      chart.setTitle(text);
		
	      
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.BLACK);
		plot.setRangeGridlinePaint(Color.WHITE);
		plot.setDomainGridlinesVisible(true);  //设置背景网格线是否可见
		//设置背景网格线颜色 plot.setDomainGridlinePaint(Color.BLACK); 
		
		// 背景色 透明度     
		plot.setBackgroundAlpha(0.5f);     
		// 前景色 透明度     
		plot.setForegroundAlpha(0.5f);
		ValueAxis rangeAxis = plot.getRangeAxis();
		
		//设置Y轴的最小值
		rangeAxis.setLowerBound((int)up-100);
		 //设置Y轴的最大值
		rangeAxis.setUpperBound((int)down+100);
	
		//折线粗细
		plot.getRenderer().setStroke(new BasicStroke(5.0f));
		
		//折线点控制
		LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.WHITE);
		
		
		
		
//		//折点 值
//		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
//		renderer.setBaseItemLabelsVisible(true);
//		
//		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));  
//		renderer.setItemLabelAnchorOffset(30D); 
//		//设置折点的颜色和字体
//	    renderer.setItemLabelPaint(Color.WHITE);
//    renderer.setItemLabelFont(new Font("Agency FB",Font.BOLD,14)); 
		
		//折线颜色
	    renderer.setBaseItemLabelsVisible(true);  
	    renderer.setSeriesFillPaint(0, new Color(77, 192, 217));  
	    renderer.setSeriesPaint(0, new Color(77, 192, 217));  
//	    renderer.setSeriesFillPaint(0, new Color(177,238,251));  
//	    renderer.setSeriesPaint(0, new Color(177,238,251));
	    renderer.setSeriesShapesVisible(0, true);  
	    renderer.setSeriesShapesVisible(1, true); 

	    
	    
	    
		
		//X軸下標字體大小及顏色
		  Axis axis1 = plot.getDomainAxis();
		  axis1.setLabelFont(new Font("微软雅黑",Font.BOLD,30));//字體
		  axis1.setLabelPaint(Color.WHITE);//顏色
	
		 
		//X軸的字體顏色
		Axis axis = plot.getDomainAxis();
		axis.setTickLabelFont(new Font("arial", Font.BOLD,12));//字體
		axis.setTickLabelPaint(Color.WHITE);//顏色 
		 

		//Y軸字下標字體大小及顏色
		Axis axis2 = plot.getRangeAxis();
		axis2.setLabelFont(new Font("微软雅黑", Font.BOLD, 30));//字體  
		axis2.setLabelPaint(Color.WHITE); //顏色
		  
		//Y軸字體及顏色
		Axis axis3 = plot.getRangeAxis();
		axis3.setTickLabelFont(new Font("arial", Font.BOLD, 12));//字體  
		axis3.setTickLabelPaint(Color.WHITE);//顏色
		

		
		
	//	categoryAxis.setMaximumCategoryLabelLines(4);
		
		CategoryAxis categoryAxis = plot.getDomainAxis();
		categoryAxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 22));

		NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
		numberaxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 22));
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		numberaxis.setAutoRangeIncludesZero(true);
		
		
		
		
	
		

		return chart;
	}

	private DefaultCategoryDataset getDataset() {

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
		up = 5000;
		down =10;
		
		for (int i =20; i >= 1; i--) {
			// 三十天不同的数据查询
			 list = gameservice.SelectUserHistoryCapable(uid,
					getpingday(90), getpingday(i));
			
			for (O2rankgame g : list) {
			//System.out.println(getpingday(i)+"之内时间段查询："+g.getDate());	
				 capable =	g.getCapable()+capable ;
			}
			
			
		temp = capable/10;
		
		if(temp>=500&&up>temp ){
			up=temp;
		}
		if(temp>=500&&down<temp){
			down = temp;
		}
			 dataset.setValue((int)capable/10, "capable", getday(i));	 
			capable=0;
		}
		
		
		
		return dataset;
	}
	
	
	//页面鼠标事件时需要的参数  
  private String hy_filename;  

   public String show(){  
    HttpServletRequest req = ServletActionContext.getRequest();  
      HttpServletResponse res = ServletActionContext.getResponse();  
        hy_filename = createChartImage(req, res);  
     return SUCCESS;  
   }   
  private String createChartImage(HttpServletRequest req, HttpServletResponse res){  
       JFreeChart chart = createChart(getDataset());  
       String filename = createUseMap(chart, 1140, 600, req, res);  
       return filename;  
  }  
  private String createUseMap(JFreeChart chart, int width, int height, HttpServletRequest req, HttpServletResponse res){  
	    //在矩形框中显示信息  
     Shape shape = new Rectangle(20, 10);  
   ChartEntity entity = new ChartEntity(shape);  
	       StandardEntityCollection coll = new StandardEntityCollection();  
       coll.add(entity);  
	       //该工具类上面没有介绍，在鼠标移动到图片时显示提示信息是用Map实现的，这些Map是用该类生成的。  
	       ChartRenderingInfo info = new ChartRenderingInfo(coll);  
	      PrintWriter pw;  
	       String filename = null;  
	      try {  
	          res.setContentType("text/html;charset=utf-8");  
	           res.setCharacterEncoding("utf-8");  
	           pw = res.getWriter();//输出MAP信息   
       
	           //写入到输出流生成图像文件，同时把图片的具体信息放入ChartRenderingInfo的一个实例为以后生成Map提供信息   
	           //ChartUtilities.writeChartAsPNG(out, chart, width, height, info);  
	         filename = ServletUtilities.saveChartAsPNG(chart, width , height, info, req.getSession());//保存图表为文件  
	            //读取info对象，生成Map信息。这些信息写在pw的输出流中，这里的输出流就是Response.out，也就是直接输出到页面了  
	          ChartUtilities.writeImageMap(pw, filename, info, false);  
	              pw.flush();  
	 
	       } catch (IOException e) {  
	             e.printStackTrace();  
	        }  
	        return filename;  
	   }  

  
  
 

	public String getpingday(int ping) {

		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -ping);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 59:59:59");
		String mDateTime = formatter.format(c.getTime());
		String time = mDateTime.substring(0, 16);//

		return time;
	}
	
	public String getday(int ping) {

		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -ping);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String mDateTime = formatter.format(c.getTime());
		String time = mDateTime.substring(0, 16);
		time = time.substring(5,time.length()-6);
		
		return time;
	}
	
	
	





	public O2rankgame getO2rankgame() {
		return o2rankgame;
	}

	public void setO2rankgame(O2rankgame o2rankgame) {
		this.o2rankgame = o2rankgame;
	}

	public O2ranktap getO2ranktap() {
		return o2ranktap;
	}

	public void setO2ranktap(O2ranktap o2ranktap) {
		this.o2ranktap = o2ranktap;
	}

	public GameService getGameservice() {
		return gameservice;
	}

	public void setGameservice(GameService gameservice) {
		this.gameservice = gameservice;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getHy_filename() {
		return hy_filename;
	}

	public void setHy_filename(String hy_filename) {
		this.hy_filename = hy_filename;
	}

}
